import { defineStore } from "pinia";

export const useComAssStore = defineStore({
  id: "compare",
  state: () => ({
    compareList: [],
    compareIndex: [],
  }),
  actions: {
    //当compareList存在两个元素时，后续添加的元素会替换掉第一个元素
    setCompareList(compareList: any) {
      if (this.compareList.length === 2) {
        this.compareList.pop();
      }
      this.compareList.push(compareList);
    },
    //设置compareIndex,最多存入两个索引值
    setCompareIndex(index: string) {
      if (this.compareIndex.length === 2) {
        this.compareIndex.pop();
      }
      this.compareIndex.push(index);
    },

    //按照index清空compareList和compareIndex
    clearCompare(index: number = -1) {
      //存在index时，删除对应的元素
      if (index !== -1) {
        //如果删除的是第一个元素，将第二个元素移到第一个元素
        if (index === 0) {
          this.compareList.shift();
          this.compareIndex.shift();
        } else {
          this.compareList.pop();
          this.compareIndex.pop();
        }
       
      } else {
        this.compareList = [];
        this.compareIndex = [];
      }
    },
  },
});
